package com.boot.web.controller;

import java.util.Date;
import java.util.Map;

import com.boot.web.dto.SpecTreeDTO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.boot.communal.paging.InitPaging;
import com.boot.communal.response.ResponseServer;
import com.boot.web.entity.GoodsInfo;
import com.boot.web.service.GoodsInfoService;

/**
 * 商品信息控制器
 * @author 【zhouJie】
 * @date 2021-09-28 17:51:24
 */
@RestController
@RequestMapping("/goodsInfo")
public class GoodsInfoController {

    @Autowired
    private GoodsInfoService goodsInfoService;

    @PostMapping("/save")
    public ResponseServer save(GoodsInfo goodsInfo) {
        goodsInfo.setSales(1);
        goodsInfo.setIsShelf("0");
        goodsInfo.setUpdateTime(new Date());
        return ResponseServer.success("添加成功", goodsInfoService.save(goodsInfo));
    }

    @PutMapping("/update")
    public ResponseServer update(GoodsInfo goodsInfo) {
        return ResponseServer.success("修改成功", goodsInfoService.update(goodsInfo));
    }

    @DeleteMapping("/delete/{id}")
    public ResponseServer delete(@PathVariable Integer id) {
        return ResponseServer.success("删除成功", goodsInfoService.delete(id));
    }

    @DeleteMapping("/batchDelete/{ids}")
    public ResponseServer batchDelete(@PathVariable Integer[] ids) {
        return ResponseServer.success("批量删除成功", goodsInfoService.batchDelete(ids));
    }

    @GetMapping("/getPageList")
    public ResponseServer getPageList(@RequestParam Map<String, Object> params) {
        InitPaging.init(params);
        return ResponseServer.success(new PageInfo(goodsInfoService.getGoodsInfoVOPageList(params)));
    }

    @GetMapping("/getAll")
    public ResponseServer getAll() {
        return ResponseServer.success(goodsInfoService.getAll());
    }

    @GetMapping("/getById/{id}")
    public ResponseServer getById(@PathVariable Integer id) {
        return ResponseServer.success(goodsInfoService.getById(id));
    }

    @GetMapping("/getGoodsSpecAttributeTree/{categoryIdAndGoodsInfoId}")
    public ResponseServer getSpecAttributeTree(@PathVariable String categoryIdAndGoodsInfoId) {
        return ResponseServer.success(goodsInfoService.getGoodsSpecAttributeTree(categoryIdAndGoodsInfoId));
    }

    @PostMapping("/submitGoodsSpecAttribute")
    public ResponseServer submitSpecAttribute(SpecTreeDTO specTreeDTO) {
        goodsInfoService.submitGoodsSpecAttribute(specTreeDTO);
        return ResponseServer.success();
    }

}
